package com.klun.tjy.tjyes.controller;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping("/test")
public class ControllerEsTest {
	Logger logger = LoggerFactory.getLogger(ControllerEsTest.class);
	@Autowired
	private TransportClient client;

	@RequestMapping("/es.do")
	public void get(){
		String index = "";
		String index_type = "";
		TermQueryBuilder mpq1 = QueryBuilders.termQuery("user_id", 1);
		QueryBuilder qb = QueryBuilders.boolQuery().must(mpq1);
		SearchRequestBuilder request = client.prepareSearch(index).setTypes(index_type);
		SearchResponse response = request.setQuery(qb)
				.setFrom(0 * 10)
				.setSize(10)
				.setPostFilter(QueryBuilders.rangeQuery("visittime").gt(""))
				.addSort("visittime", SortOrder.DESC)
				.execute().actionGet(5, TimeUnit.SECONDS);
		SearchHits searchHits = response.getHits();
		SearchHit[] hits = searchHits.getHits();
		for (SearchHit hit : hits) {

		}
		logger.info("success");
	}

}
